﻿using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;

namespace equinoxWeb.GestionConexion
{
    class GestorConexion
    {

        private MySqlConnection connection;

        public MySqlConnection Connection
        {
            get { return connection; }
            set { connection = value; }
        }
        private string server;
        private string database;
        private string uid;
        private string password;

        //Constructor
        public GestorConexion()
        {
            Initialize();
        }

        //Initialize values
        private void Initialize()
        {
            /*
            server = "localhost";//"equinoxdb.cyzvl9dqrda7.us-east-1.rds.amazonaws.com";
            database = "equinox";// "ilushen_equinox"//"equinox";
            uid = "root";// "ilush_equinoxdba" // "dbauser";
            password = "lilchise"; // "Jwjw4437";
            */
            server = "localhost";// "equinoxdb.cyzvl9dqrda7.us-east-1.rds.amazonaws.com";
                        database = "equinox";// "ilushen_equinox";
                        uid = "dbauser";// "ilush_equinoxdba";
                        password = "Jwjw4437";
            
            string connectionString;
            connectionString = "SERVER=" + server + ";" + "DATABASE=" +
            database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

            connection = new MySqlConnection(connectionString);
        }
        //open connection to database
        public bool OpenConnection()
        {
            try
            {
                connection.Open();
                return true;
            }
            catch (MySqlException ex)
            {
                //When handling errors, you can your application's response based 
                //on the error number.
                //The two most common error numbers when connecting are as follows:
                //0: Cannot connect to server.
                //1045: Invalid user name and/or password.
                switch (ex.Number)
                {
                    case 0:
                        break;

                    case 1045:
                        break;
                }
                return false;
            }
        }

        //Close connection
        public bool CloseConnection()
        {
            try
            {
                connection.Close();
                return true;
            }
            catch (MySqlException ex)
            {
                return false;
            }
        }

        public static List<int> GetListaAnhos(int nAnhos)
        {
            GestorConexion gestor = new GestorConexion();
            gestor.OpenConnection();
            MySqlConnection con = gestor.Connection;
            MySqlCommand cmd;
            List<int> listaAnhos = new List<int>();
            int anho = 0;
            cmd = new MySqlCommand("select CAST(DATE_FORMAT(NOW(),'%Y') AS UNSIGNED) ", con);
            cmd.Prepare();
            MySqlDataReader res = cmd.ExecuteReader();
            if (res.Read())
            {
                anho = res.GetInt32(0);
            }
            else
            {
                anho = 2014;
            }
            for (int i = 0; nAnhos > 0; nAnhos--)
            {
                listaAnhos.Add(anho - i);
                i++;
            }
            res.Close();
            gestor.CloseConnection();
            return listaAnhos;
        }
    }
}
